package com.yangluyao.easyexcel.utils;

import com.xiaoleilu.hutool.cache.CacheUtil;
import com.xiaoleilu.hutool.cache.impl.TimedCache;
import com.xiaoleilu.hutool.date.DateUnit;

/**
 * 缓存返回值，针对没有返回值的方法
 *
 * @author Brain
 */
public class CacheResponse {


    final static ApiResponse responseEntity = new ApiResponse();


    //创建缓存，默认3分钟过期
    final static TimedCache<String, ApiResponse> timedCache = CacheUtil.newTimedCache(DateUnit.MINUTE.getMillis() * 3);


    public static ApiResponse build(String key) {
        //默认值
        responseEntity.setStatus(SuccessStatus.OPERATION_SUCCESS.status());
        responseEntity.setMessage(SuccessStatus.OPERATION_SUCCESS.message());
        //实例化创建
        timedCache.put(key, responseEntity);
        //启动定时任务，每3000毫秒秒检查一次过期
        timedCache.schedulePrune(3000);
        return responseEntity;
    }

    public static ApiResponse getResponseEntity(String key) {
        if (timedCache.containsKey(key)) {
            return  timedCache.get(key);
        }
        return null;
    }

    public static void setResponseEntity(String key,ApiResponse apiResponse) {
        timedCache.put(key, apiResponse);
    }
}
